﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using Catering_Rules;
using System.Data;

namespace Catering.Services
{
    public class CommonServices : IAjaxService
    {
        public AjaxResult Excute(HttpContext httpContext)
        {
            string FunType = httpContext.Request["FunType"];
            AjaxResult result = null;
            switch (FunType.ToLower())
            {
                case "notepad":
                    result = LoadNotePad(httpContext);
                    break;
            }
            return result;
        }

        private string[] weekDays = { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        private AjaxResult LoadNotePad(HttpContext context)
        {
            string dataType = context.Request["datatype"];
            string type = context.Request["type"];
            var res = new ArrayList();
            string strdatetime = DateTime.Now.ToString("yyyy年MM月dd日") + "    " + weekDays[(int)DateTime.Now.DayOfWeek] + "    " + DateTime.Now.ToString("HH:mm");
            switch (dataType)
            {
                case "simple":
                    //获取显示内容
                    Catering_Model.Announcement an = new Catering_Rules.Announcement().GetZhanBanModel();
                    if (an != null && !string.IsNullOrEmpty(an.Conent)) res.Add(new { body = an.Conent, datetime = strdatetime });
                    else res.Add(new { body = "", datetime = strdatetime });
                    break;
                case "list":
                    Dictionary<string, List<string[]>> dic = GetZhanBanPerson();
                    var list1 = new ArrayList();
                    if (dic != null && dic.Count > 0)
                    {
                        int maxnum = 0;
                        foreach (string key in dic.Keys)
                        {
                            maxnum = maxnum > dic[key].Count ? maxnum : dic[key].Count;
                        }
                        for (int i = 0; i < maxnum; i++)
                        {
                            string ClassName1 = "", Steward1 = "", SecurityU1 = "", ClassName2 = "", Steward2 = "", SecurityU2 = "", ClassName3 = "", Steward3 = "", SecurityU3 = "", ClassName4 = "", Steward4 = "", SecurityU4 = "", ClassName5 = "", Steward5 = "", SecurityU5 = "";

                            if (dic.ContainsKey("渤西作业区") && dic["渤西作业区"].Count > i)
                            {
                                ClassName1 = dic["渤西作业区"][i][0];
                                Steward1 = dic["渤西作业区"][i][1];
                                SecurityU1 = dic["渤西作业区"][i][2];
                                if (ClassName1 == "QK18-1")
                                {
                                    ClassName1 = "QK18-1<br/>QK18-2";
                                }
                            }
                            if (dic.ContainsKey("辽东作业区") && dic["辽东作业区"].Count > i)
                            {
                                ClassName2 = dic["辽东作业区"][i][0];
                                Steward2 = dic["辽东作业区"][i][1];
                                SecurityU2 = dic["辽东作业区"][i][2];
                            }
                            if (dic.ContainsKey("渤南作业区") && dic["渤南作业区"].Count > i)
                            {
                                ClassName3 = dic["渤南作业区"][i][0];
                                Steward3 = dic["渤南作业区"][i][1];
                                SecurityU3 = dic["渤南作业区"][i][2];
                            }
                            if (dic.ContainsKey("绥中作业区") && dic["绥中作业区"].Count > i)
                            {
                                ClassName4 = dic["绥中作业区"][i][0];
                                Steward4 = dic["绥中作业区"][i][1];
                                SecurityU4 = dic["绥中作业区"][i][2];
                            }
                            if (dic.ContainsKey("渤中作业区") && dic["渤中作业区"].Count > i)
                            {
                                ClassName5 = dic["渤中作业区"][i][0];
                                Steward5 = dic["渤中作业区"][i][1];
                                SecurityU5 = dic["渤中作业区"][i][2];
                            }
                            list1.Add(
                                new
                                {
                                    ClassName1 = ClassName1,
                                    Steward1 = Steward1,
                                    SecurityU1 = SecurityU1,
                                    ClassName2 = ClassName2,
                                    Steward2 = Steward2,
                                    SecurityU2 = SecurityU2,
                                    ClassName3 = ClassName3,
                                    Steward3 = Steward3,
                                    SecurityU3 = SecurityU3,
                                    ClassName4 = ClassName4,
                                    Steward4 = Steward4,
                                    SecurityU4 = SecurityU4,
                                    ClassName5 = ClassName5,
                                    Steward5 = Steward5,
                                    SecurityU5 = SecurityU5
                                });
                        }
                    }
                    res.Add(new { list1 = list1, datetime = strdatetime });
                    break;
            }
            return new AjaxResult(true, res);
        }

        private Dictionary<string, List<string[]>> GetZhanBanPerson()
        {
            DateTime time = DateTime.Now.AddDays(-1);
            Dictionary<string, List<string[]>> dic = new Dictionary<string, List<string[]>>();
            DataTable dt = new Catering_Rules.AttendanceRecord().GetList(" Date='" + time.ToString("yyyy-MM-dd") + "' and State=1 and (temp1='1' or temp2='1')").Tables[0];
            DataTable dt_class = new Catering_Rules.Company().GetView_CompanyList(" AND CompanyID=1 AND CName<>'自营公司' ").Tables[0];
            var p = from d in dt.AsEnumerable()
                    select new
                    {
                        pname = d.Field<string>("PName"),
                        classid = d.Field<int>("ClassID"),
                        temp1 = d.Field<string>("temp1"),
                        temp2 = d.Field<string>("temp2")
                    };
            if (dt_class != null && dt_class.Rows.Count > 0)
            {
                for (int i = 0; i < dt_class.Rows.Count; i++)
                {
                    string areaname = dt_class.Rows[i]["AreaName"].ToString();
                    int classid = Convert.ToInt32(dt_class.Rows[i]["ClassID"]);
                    string classname = dt_class.Rows[i]["CName"].ToString();
                    string guanshi = "";
                    string anquanyuan = "";
                    if (classname == "采购食堂")
                    {
                        guanshi = "冀伏堂";
                        anquanyuan = "李德峰";
                    }
                    else if (classname == "QK18-2")
                    {
                        continue;
                    }
                    else
                    {
                        var t = p.ToList().Where(o => o.classid == classid);
                        t.ToList().ForEach(s =>
                        {
                            if (s.temp1 != null && s.temp1.Equals("1")) guanshi = s.pname;
                            if (s.temp2 != null && s.temp2.Equals("1")) anquanyuan = s.pname;
                        });

                        if (string.IsNullOrEmpty(guanshi) && string.IsNullOrEmpty(anquanyuan))
                        {
                            DataTable dt_temp = new Catering_Rules.AttendanceRecord().GetList(" ClassID=" + classid + " AND Date='" + time.AddDays(-1).ToString("yyyy-MM-dd") + "' and State=1 and (temp1='1' or temp2='1')").Tables[0];
                            if (dt_temp != null && dt_temp.Rows.Count > 0)
                            {
                                foreach (DataRow item in dt_temp.Rows)
                                {
                                    string t_temp1 = item["temp1"] is DBNull ? "" : item["temp1"].ToString();
                                    string t_temp2 = item["temp2"] is DBNull ? "" : item["temp2"].ToString();
                                    string t_pname = item["PName"] is DBNull ? "" : item["PName"].ToString();
                                    if (t_temp1 == "1" && string.IsNullOrEmpty(guanshi))
                                    {
                                        guanshi = t_pname;
                                    }
                                    if (t_temp2 == "1" && string.IsNullOrEmpty(anquanyuan))
                                    {
                                        anquanyuan = t_pname;
                                    }
                                }
                                dt_temp.Dispose();
                            }
                        }
                    }
                    if (dic.ContainsKey(areaname))
                    {
                        dic[areaname].Add(new string[3] { classname, guanshi, anquanyuan });
                    }
                    else
                    {
                        List<string[]> list = new List<string[]>();
                        list.Add(new string[3] { classname, guanshi, anquanyuan });
                        dic.Add(areaname, list);
                    }
                }
            }
            dt_class.Dispose();
            dt.Dispose();
            return dic;
        }
    }
}
